package com.shengbangtec.service.impl;

import com.shengbangtec.mapper.TBackSalaryLawMapper;
import com.shengbangtec.model.TBackSalaryClueDO;
import com.shengbangtec.mapper.TBackSalaryClueMapper;
import com.shengbangtec.service.TBackSalaryClueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengbangtec.vo.CaseManagerVO;
import com.shengbangtec.vo.ClueInfoEveryDayVO;
import com.shengbangtec.vo.ClueInfoForCityVO;
import com.shengbangtec.vo.ClueInfoForMapVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.security.auth.callback.LanguageCallback;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LBY
 * @since 2021-07-05
 */
@Service
public class TBackSalaryClueServiceImpl extends ServiceImpl<TBackSalaryClueMapper, TBackSalaryClueDO> implements TBackSalaryClueService {

    @Autowired
    private TBackSalaryClueMapper tBackSalaryClueMapper;



    /**
     * 建设领域饼状图
     * @return
     */
    @Override
    public Map<String, Object> getConstructionField(String type) {
        return tBackSalaryClueMapper.getConstructionField(type);
    }

    /**
     * 待接收线索
     * @return
     */
    @Override
    public List<Map<String, Object>> getStayReceiveClues() {
        return tBackSalaryClueMapper.getStayReceiveClues();
    }

    /**
     * 接受线索条数/涉及总金额
     * @param type 1:接受线索条数，2：涉及总金额
     * @return
     */
    @Override
    public List<Map<String, Object>> getNumbersOfClues(String type) {
        List<Map<String, Object>> list = null;
        if(type.equals("1")){
            list = tBackSalaryClueMapper.getNumbersOfClues();
           // list = getNumbersOfClues();
        }else if(type.equals("2")){
            list = tBackSalaryClueMapper.getInvolvingSumAmount();
        }
        return list;
    }

    /**
     * 时间范围
     * @param type 1:全部、2：10天内未处理、3：10-20天内未处理、4：超过20天未处理、5：涉及总金额、6：昨天-前30天
     * @return
     */
    public Map<String,Object> commentConditionMap(Integer type){
        Map<String,Object> map = new HashMap();
        Integer amount = -10;
        Calendar c = Calendar.getInstance();
        if(type == null){
            type = 2;
        }
        //10天内未处理
        if (type == 2)
        {
            map.put("dateBeginTime",getDateBegin(c,-10)+" 00:00:00");
        }
        //10-20天内未处理
        if (type == 3)
        {
            map.put("dateEndTime",getDateBegin(c,amount)+" 23:59:59");
            map.put("dateBeginTime",getDateBegin(c,amount)+" 00:00:00");
        }
        //超过20天未处理
        if (type == 4)
        {
            amount = -20;
            map.put("dateBeginTime",getDateBegin(c,amount)+" 23:59:59");
        }
        //昨天-前15天
        if (type == 6)
        {
            map.put("dateEndTime",getDateBegin(c,-1));
            map.put("dateBeginTime",getDateBegin(c,-14));
        }
        return map;
    }

    public String getDateBegin(Calendar c,Integer amount){
        String dateBegin = "2000-01-01";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        c.add(Calendar.DATE,amount);
        dateBegin = formatter.format(c.getTime());
        return dateBegin;
    }

    /**
     * 石家庄线索详情
     * @return
     */
    @Override
    public ClueInfoForCityVO clueInfoForCity() {
        ClueInfoForCityVO clueInfoForCityVO = tBackSalaryClueMapper.clueInfoForCity();
        return clueInfoForCityVO;
    }

    /**
     * 地图
     * @return
     */
    @Override
    public List<ClueInfoForMapVO> clueInfoForMap() {
        List<ClueInfoForMapVO> clueInfoForMapVO = tBackSalaryClueMapper.clueInfoForMap();
        return clueInfoForMapVO;
    }

    /**
     *  每日线索走势
     * @return
     */
    @Override
    public List<ClueInfoEveryDayVO> clueInfoEveryDay(String areaId) {
        Map<String, Object> param = commentConditionMap(6);
        param.put("areaId",areaId);
        //查询表中每天的新增线索数
        List<ClueInfoEveryDayVO> clueInfoEveryDayVOList = tBackSalaryClueMapper.clueInfoEveryDay(param);
        Map<LocalDate, ClueInfoEveryDayVO> map = new HashMap<>();
        //map中放入15天的空数据
        for (int i = 1; i <= 15; i++) {
            LocalDate clueDate = LocalDate.now().plusDays(-i);
            ClueInfoEveryDayVO infoVO = new ClueInfoEveryDayVO(0,clueDate);
            map.put(clueDate, infoVO);
        }
        //将表中的的线索数替换掉空数据
        for (ClueInfoEveryDayVO info : clueInfoEveryDayVOList) {
            LocalDate clueDate = info.getClueDate();
            map.put(clueDate, info);
        }
        //将得到的map转换为list
        List<ClueInfoEveryDayVO> list = new ArrayList(map.values());
        //正序排列
        list.sort(Comparator.comparing(ClueInfoEveryDayVO::getClueDate));
        return list;
    }

    /**
     *  办结线索对比
     * @return
     */
    @Override
    public Map<String,Object> handleCountCompare(String areaId) {
        Map<String, Object> rmap = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        int lastYear = LocalDate.now().plusYears(-1).getYear();
        int thisYear = LocalDate.now().getYear();
        param.put("lastYear",lastYear);
        param.put("thisYear",thisYear);
        if (!StringUtils.isEmpty(areaId)) {
            param.put("areaId",areaId);
        }
        List<BigDecimal> lastList = new ArrayList<>();
        List<BigDecimal> thisList = new ArrayList<>();
        Map<String,BigDecimal> handleCountCompareLast = tBackSalaryClueMapper.handleCountCompareLast(param);
        if (handleCountCompareLast != null) {
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT01"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT02"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT03"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT04"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT05"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT06"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT07"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT08"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT09"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT10"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT11"));
            lastList.add(handleCountCompareLast.get("HANDLEFINISHCOUNT12"));
        }
        Map<String,BigDecimal> handleCountCompareThis= tBackSalaryClueMapper.handleCountCompareThis(param);
        int monthCount = LocalDate.now().plusMonths(-1).getMonthValue();
        if (handleCountCompareThis!=null&&monthCount!=12) {
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT01"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT02"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT03"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT04"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT05"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT06"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT07"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT08"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT09"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT10"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT11"));
            thisList.add(handleCountCompareThis.get("HANDLEFINISHCOUNT12"));
            for (int i = 11; i >= monthCount; i--) {
                thisList.remove(i);
            }
        }
        rmap.put("handleCountCompareLast", lastList);
        rmap.put("handleCountCompareThis", thisList);
        rmap.put("lastYear", lastYear+"年");
        rmap.put("thisYear", thisYear+"年");
        return rmap;
    }

    /**
     *  已解决人数对比
     * @return
     */
    @Override
    public Map<String, Object> handlePeopleCompare(String areaId) {
        Map<String, Object> rmap = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        int lastYear = LocalDate.now().plusYears(-1).getYear();
        int thisYear = LocalDate.now().getYear();
        param.put("lastYear",lastYear);
        param.put("thisYear",thisYear);
        if (!StringUtils.isEmpty(areaId)) {
            param.put("areaId",areaId);
        }
        List<BigDecimal> lastList = new ArrayList<>();
        List<BigDecimal> thisList = new ArrayList<>();
        Map<String,BigDecimal> handlePeopleCompareLast = tBackSalaryClueMapper.handlePeopleCompareLast(param);
        if (handlePeopleCompareLast != null) {
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE01"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE02"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE03"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE04"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE05"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE06"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE07"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE08"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE09"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE10"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE11"));
            lastList.add(handlePeopleCompareLast.get("HANDLEFINISHPEOPLE12"));
        }
        Map<String,BigDecimal> handlePeopleCompareThis= tBackSalaryClueMapper.handlePeopleCompareThis(param);
        int monthCount = LocalDate.now().plusMonths(-1).getMonthValue();
        if (handlePeopleCompareThis!=null&&monthCount!=12) {
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE01"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE02"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE03"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE04"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE05"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE06"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE07"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE08"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE09"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE10"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE11"));
            thisList.add(handlePeopleCompareThis.get("HANDLEFINISHPEOPLE12"));
            for (int i = 11; i >= monthCount; i--) {
                thisList.remove(i);
            }
        }
        rmap.put("handlePeopleCompareLast", lastList);
        rmap.put("handlePeopleCompareThis", thisList);
        rmap.put("lastYear", lastYear+"年");
        rmap.put("thisYear", thisYear+"年");
        return rmap;
    }

    /**
     *  已解决总金额对比
     * @return
     */
    @Override
    public Map<String, Object> handleAmountCompare(String areaId) {
        Map<String, Object> rmap = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        int lastYear = LocalDate.now().plusYears(-1).getYear();
        int thisYear = LocalDate.now().getYear();
        param.put("lastYear",lastYear);
        param.put("thisYear",thisYear);
        if (!StringUtils.isEmpty(areaId)) {
            param.put("areaId",areaId);
        }
        List<BigDecimal> lastList = new ArrayList<>();
        List<BigDecimal> thisList = new ArrayList<>();
        Map<String,BigDecimal> handleAmountCompareLast = tBackSalaryClueMapper.handleAmountCompareLast(param);
        if (handleAmountCompareLast != null) {
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT01"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT02"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT03"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT04"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT05"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT06"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT07"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT08"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT09"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT10"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT11"));
            lastList.add(handleAmountCompareLast.get("HANDLEFINISHAMOUNT12"));
        }
        Map<String,BigDecimal> handleAmountCompareThis= tBackSalaryClueMapper.handleAmountCompareThis(param);
        int monthCount = LocalDate.now().plusMonths(-1).getMonthValue();
        if (handleAmountCompareThis!=null&&monthCount!=12) {
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT01"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT02"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT03"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT04"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT05"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT06"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT07"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT08"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT09"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT10"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT11"));
            thisList.add(handleAmountCompareThis.get("HANDLEFINISHAMOUNT12"));
            for (int i = 11; i >= monthCount; i--) {
                thisList.remove(i);
            }
        }
        rmap.put("handleAmountCompareLast", lastList);
        rmap.put("handleAmountCompareThis", thisList);
        rmap.put("lastYear", lastYear+"年");
        rmap.put("thisYear", thisYear+"年");
        return rmap;
    }


}
